package cmdutil

import (
	"errors"
	"fmt"
	"hcy-api/structs/base"
	"path/filepath"
	"time"
	"vap_host/global"
)

func RunPipe(conf base.PluginPipeInstruct) (err error) {
	option := CommandOption{
		User: conf.User,
		Env:  conf.Env,
	}
	argus := make([]string, 0)
	argus = append(argus, filepath.Join(global.Conf.AppFolder, "plugin", conf.Cmd))
	argus = append(argus, conf.Args...)
	RunCommandArgsForPipe(argus, option, conf.OutChan, conf.CloseChan, conf.Cancel)
	return
}

func Run(conf base.PluginInstruct) (stdout string, err error) {
	option := CommandOption{
		TimeoutDuration: time.Duration(conf.TimeoutDuration) * time.Second,
		User:            conf.User,
		Env:             conf.Env,
	}
	argus := make([]string, 0)
	argus = append(argus, filepath.Join(global.Conf.AppFolder, "plugin", conf.Cmd))
	argus = append(argus, conf.Args...)
	stdout, stderr, err := RunCommandArgs(argus, option)
	if err != nil {
		err = errors.New(fmt.Sprintf("插件调用时发生错误:[err: %v ],[stdout:%s],[stderr:%s]", err, stdout, stderr))
		return
	}

	return stdout, nil
}
